package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.github.pagehelper.PageInfo;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.VideoComment;
import com.zzgele.qmzb.api.service.VideoCommentService;
import com.zzgele.qmzb.api.utils.DateUtil;
import com.zzgele.qmzb.api.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Martin on 2017/07/13.
*/
@RestController
@RequestMapping("/video_comment")
public class VideoCommentController {
    private final Logger logger = LoggerFactory.getLogger(VideoCommentController.class);
    @DubboConsumer
    private VideoCommentService videoCommentService;

    @PostMapping("/create")
    public Result create(Long login_uid, Long video_id, String content) {

        Condition condition = new Condition(VideoComment.class);
        condition.createCriteria().andCondition("uid=" + login_uid + " AND vid=" + video_id);
        List<VideoComment> videoCommentList = videoCommentService.findByCondition(condition);
        if(videoCommentList.size() != 0) {
            return ResultGenerator.genFailResult("提交失败，不能重复评论一个信息");
        }

        VideoComment videoComment = new VideoComment();
        videoComment.setCreateTime(DateUtil.getCurUnixTimeStampInt());
        videoComment.setUid(login_uid);
        videoComment.setVid(video_id);
        videoComment.setContent(content);
        videoComment.setStatus(1); //自动审核
        videoCommentService.save(videoComment);

        return ResultGenerator.genSuccessResult("提交成功!");
    }

    @PostMapping("/list") //我的关注=我关注的=(FANS_UID=我)
    public Result follow_list(Long login_uid, Long video_id, Integer page, Integer page_size) {
        PageUtil.startPage(page, page_size);
        Map<String, Object> map = new HashMap();
        map.put("uid", login_uid);
        map.put("vid", video_id);
        List<HashMap> videoCommentList = videoCommentService.getList(map);
        HashMap followListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(videoCommentList));

        return ResultGenerator.genSuccessResult(followListHashMap);
    }
}
